/*发送消息*/
function send(headSrc,str){
	var html="<div class='send'>" +
		"<div class='time'>"+getNowFormatDate()+"</div>" +
		"<div class='msg'><img src="+headSrc+" />"+
	"<p>"+str+"</p></div></div>";
	upView(html);
}

/*接受消息*/
function show(headSrc,str){
	var html="<div class='show_L'>" +
		"<div class='time'>"+getNowFormatDate()+"</div>" +
		"<div class='msg'>"+"<img src="+headSrc+"/>"+
		"<p>"+str+"</p></div></div>";
	upView(html);
}

//获取日期
function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
		+ " " + date.getHours() + seperator2 + date.getMinutes()
		+ seperator2 + date.getSeconds();
	return currentdate;
}

/*更新视图*/
function upView(html){
	$('.message').append(html);
	$('.mui-scroll-wrapper').animate({scrollTop:$('.message').outerHeight()-window.innerHeight+200},100)
}


$('body').delegate('.input_val', 'keyup', function(){
	if($(this).val().length>0){
		$(this).next().css('background','#999');
	}else{
		$(this).next().css('background','#ddd');
	}
});

$('body').delegate('.Send_out', 'click', function(){
	if($(".input_val").val().length>0){
		var msg = $(".input_val").val();
		test();
		var utel = getLoginPhone();
		var ftel = $('.my_page_title').attr('value');
		$.ajax({
			type: 'POST',
			url: webConfig.url+'/Home/friends/sendMsg',
			data: {msg: msg, utel: utel, ftel: ftel},
			dataType: 'JSONP',
			crossDomain: true,
			success: function(data){
				$(".input_val").val("");
				$('.Send_out').css('background','#ddd');
				if (data.msg == 1) {
					show(imgarr[1],msg);
				} else if (data.msg == 0) {
					mui.toast('发送失败');
				}
			}
		});
	}else{
		mui.toast("不能发送空消息")
	}
});


var arr=['我是小Q'];
var imgarr=['images/beike_da.png','images/dazhong.png'];
function test(){
	$(arr).each(function(i){
		setTimeout(function(){
			send(imgarr[0],arr[i])
		},300)
	})
}

//发送表情
$('body').delegate('.biaoqing_img', 'click', function(e){
	$(".biaoqing-photo").toggle();
	e.stopPropagation();
});

$(document).on("click",function(){
	$(".biaoqing-photo").css("display", "none");
});

$('body').delegate('.emoji-picker-image', 'click', function(){
	var bq = $(this).parent().html();
	// console.log(bq);
	$(".message").append("<div class='show_L'>" +
	"<div class='time'>"+getNowFormatDate()+"</div>" +
	"<div class='msg'><img src="+imgarr[1]+" />"+
	"<p>"+bq+"</p></div></div>");
	//发送后关闭表情框
	$(".biaoqing-photo").toggle();
	test()
});

$('body').delegate('.chat-list-people', 'click', function(){
	$('.chat_item').toggleClass('hide');
	$(".chat-list-people").toggleClass('show');
	var index = $(this).attr('val');
	$('.car_detail_header_icon').text(friends[index].nickname);
	$('.my_page_title').attr('value',friends[index].tel);
	var utel = getLoginPhone();
	$.ajax({
		type: 'POST',
		url: webConfig.url+'/Home/friends/selectMsg',
		data: {utel: utel, ftel: friends[index].tel},
		dataType: 'JSONP',
		crossDomain: true,
		success: function(data){
			console.log(data);
		}
	});

	mui('.mui-scroll-wrapper').scroll({
        deceleration: 0.005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});
	
});

$('body').delegate('.back_img', 'click', function(){
	$('.chat_item').addClass("hide").removeClass("show");
	$(".chat_box").addClass("show").removeClass("hide");
});

$('body').delegate('.add_span', 'click', function(){
	$(".add_friend").addClass("show").removeClass("hide");
	$(".chat_box").addClass("hide").removeClass("show");
});


//查看信息
$("body").delegate(".send .msg img","click",function(){
	$(".first_data").addClass("show").removeClass("hide");
	$('.chat_item').addClass("hide").removeClass("show");
	$(".chat_box").addClass("hide").removeClass("show");
	$.ajax({
		type: 'POST',
		url: webConfig.url+'/Home/friends/',
	});
});

$("body").delegate(".show_L .msg img","click",function(){
	$(".first_data").addClass("show").removeClass("hide");
	$('.chat_item').addClass("hide").removeClass("show");
});

$('body').delegate('.chat_ll', 'click', function(){
	$(".first_data").addClass("hide").removeClass("show");
	$('.chat_item').addClass("show").removeClass("hide");
});

$('body').delegate('.add_span_k', 'click', function(){
	var tel = $('.add_seach input').val();
	var mobilephone = getLoginPhone();
	if (tel.length != 11) {
		mui.toast('手机号位数不正确');
		return;
	}

	$.ajax({
		type: 'POST',
		url: webConfig.url+'/Home/friends/addFriends',
		data: {tel: tel, mobilephone: mobilephone},
		dataType: 'JSONP',
		crossDomain: true,
		success: function(data){
			if (data.msg) {
				mui.toast(data.msg);
			}
		}

	});
	
});


